﻿using System;
using System.Net;
using System.Windows;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace Forrst
{
    public class JsonUser
    {
        // {"resp":{}}
        public Resp resp;
    }

    public class Resp
    {
        // {"stat":"ok","user":{}}
        public User user; // {}
        public string stat; // "ok"
    }
    public class User
    {
        // {"id":"2682","username":"Templarian"}
        public string id;
        public string username;
    }

    public class ForrstUser
    {
        public string Username;
        public int Id;
    }

    public class ForrstRequest
    {
        private string address = "http://api.forrst.com/api/";
        private string version = "v1";
        public string Username { get; set; }
        public int Id { get; set; }
        
        public ForrstRequest()
        {
            
        }

        public ForrstRequest(string username, string password)
        {
            Username = username;
        }

        public delegate void ForrstHandler(ForrstUser user);
        public event ForrstHandler GetUserInfoCompleted;
        public void GetUserInfoAsync(int usernameId)
        {
            ForrstUser forrstUser = new ForrstUser();
            Uri serviceUri = new Uri(address + version + "/users/info?id=" + usernameId);
            WebClient downloader = new WebClient();
            downloader.OpenReadCompleted += (x, y) =>
            {
                if (y.Error == null)
                {
                    ForrstUser user = new ForrstUser();
                    Stream responseStream = y.Result;
                    // Show JSON File
                    // StreamReader sr = new StreamReader(responseStream);
                    // MessageBox.Show(sr.ReadToEnd());
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonUser));
                    JsonUser tempUser = (JsonUser)serializer.ReadObject(responseStream);
                    user.Id = Convert.ToInt32(tempUser.resp.user.id);
                    user.Username = tempUser.resp.user.username;
                    InvokeGetUserInfoCompleted(user);
                }
            };
            downloader.OpenReadAsync(serviceUri);
            
        }
        
        public void InvokeGetUserInfoCompleted(ForrstUser user)
        {
            ForrstHandler handler = GetUserInfoCompleted;
            if (handler != null) handler(user);
        }
        public void GetUserInfoAsync(string username)
        {

            ForrstUser forrstUser = new ForrstUser();
            Uri serviceUri = new Uri(address + version + "/users/info?username=" + username);
            WebClient downloader = new WebClient();
            downloader.OpenReadCompleted += (x, y) =>
            {
                if (y.Error == null)
                {
                    ForrstUser user = new ForrstUser();
                    Stream responseStream = y.Result;
                    // Show JSON File
                    // StreamReader sr = new StreamReader(responseStream);
                    // MessageBox.Show(sr.ReadToEnd());
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonUser));
                    JsonUser tempUser = (JsonUser)serializer.ReadObject(responseStream);
                    user.Id = Convert.ToInt32(tempUser.resp.user.id);
                    user.Username = tempUser.resp.user.username;
                    InvokeGetUserInfoCompleted(user);
                }
            };
            downloader.OpenReadAsync(serviceUri);
        }
        
    }
}
